package cn.eeepay.framework.dao;


import cn.eeepay.boss.system.CommonUtilAbstract;
import cn.eeepay.framework.db.pagination.Page;
import cn.eeepay.framework.model.MerTerChangeBind;
import cn.eeepay.framework.model.TerminalInfo;
import cn.eeepay.framework.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
import java.util.Map;

public interface MerTerChangeBindDao {


    @SelectProvider(type = SqlProvider.class, method = "selectMerTerChangeBindPage")
    @ResultType(MerTerChangeBind.class)
    List<MerTerChangeBind> selectMerTerChangeBindPage(@Param("page") Page<MerTerChangeBind> page,
                                    @Param("baseInfo") MerTerChangeBind baseInfo);


    @Select("select * from merchant_terminal_change_bind where id=#{id}")
    @ResultType(MerTerChangeBind.class)
    MerTerChangeBind selectMerTerChangeBindById(@Param("id") String id);

    @Update("update merchant_terminal_change_bind set change_status=#{merTerChangeBind.changeStatus},rebind_remark=#{merTerChangeBind.rebindRemark} where  id = #{merTerChangeBind.id}")
    int updateChangeStatus(@Param("merTerChangeBind") MerTerChangeBind merTerChangeBind);

    @Update("update merchant_terminal_change_bind set rebind_remark=#{merTerChangeBind.rebindRemark} where  id = #{merTerChangeBind.id}")
    int updateRebindRemark(@Param("merTerChangeBind") MerTerChangeBind merTerChangeBind);


    @Select("SELECT * FROM terminal_info WHERE sn=#{sn} and merchant_no=#{merchantNo}")
    @ResultType(TerminalInfo.class)
    TerminalInfo selectBySnAndMerNo(@Param("sn") String sn,@Param("merchantNo") String merchantNo);

    class SqlProvider {
        public String selectMerTerChangeBindPage(Map<String, Object> param) {
            MerTerChangeBind baseInfo = (MerTerChangeBind) param.get("baseInfo");
            SQL sql = new SQL();
            sql.SELECT("mtcb.*" +
                    ",mi.merchant_name" +
                    ",ai.agent_name" +
                    ",oneAi.agent_name as oneLevelAgentName"+
                    ",operAi.agent_name as operAgentName"
            );
            sql.FROM(fromSql());
            whereSql(baseInfo, sql);
            sql.ORDER_BY("mtcb.create_time desc");
            return sql.toString();
        }




        public String fromSql() {
            StringBuilder sb = new StringBuilder(" merchant_terminal_change_bind mtcb ");
            sb.append(" LEFT JOIN merchant_info mi on mi.merchant_no=mtcb.merchant_no ");
            sb.append(" LEFT JOIN agent_info ai on ai.agent_no=mtcb.agent_no ");
            sb.append(" LEFT JOIN agent_info oneAi on oneAi.agent_no=mtcb.one_level_agent ");
            sb.append(" LEFT JOIN agent_info operAi on operAi.agent_no=mtcb.oper_agent ");

            return sb.toString();
        }

        public void whereSql(MerTerChangeBind param, SQL sql) {
            if (param == null) {
                return;
            }
            if (!StringUtil.isEmpty(param.getMerchantNo())) {
                sql.WHERE("mtcb.merchant_no = #{baseInfo.merchantNo}");
            }
            if (!StringUtil.isEmpty(param.getOldSn())) {
                sql.WHERE("mtcb.old_sn = #{baseInfo.oldSn}");
            }
            if (!StringUtil.isEmpty(param.getNewSn())) {
                sql.WHERE("mtcb.new_sn = #{baseInfo.newSn}");
            }
            if (!StringUtil.isEmpty(param.getAgentNo())) {
                sql.WHERE("mtcb.agent_no = #{baseInfo.agentNo}");
            }
            if (!StringUtil.isEmpty(param.getOneLevelAgent())) {
                sql.WHERE("mtcb.one_level_agent = #{baseInfo.oneLevelAgent}");
            }
            if (!StringUtil.isEmpty(param.getChangeStatus())) {
                //换绑状态: 1=换绑成功,2=换绑失败(需要回绑旧机具),3=换绑失败(其他),4=回绑成功，页面显示1=换绑成功，其他=换绑失败
                //传1查change_status=1，传2查change_status=2，3，4
                if("1".equals(param.getChangeStatus())){
                    sql.WHERE("mtcb.change_status = #{baseInfo.changeStatus}");
                }else{
                    sql.WHERE("mtcb.change_status in (2,3,4)");
                }

            }
            if (!StringUtil.isEmpty(param.getSynBindStatus())) {
                //是否同步绑定。0-否，1是。0-否对应changeStatus=2，1-是对应changeStatus=1，3，4
                //传0查change_status=2，传1查change_status=1，3，4
                if("0".equals(param.getSynBindStatus())){
                    sql.WHERE("mtcb.change_status = 2");
                }else{
                    sql.WHERE("mtcb.change_status in (1,3,4)");
                }

            }
            if (!StringUtil.isEmpty(param.getStartTime())) {
                sql.WHERE("mtcb.create_time>=#{baseInfo.startTime}");
            }
            if (!StringUtil.isEmpty(param.getEndTime())) {
                sql.WHERE("mtcb.create_time<=#{baseInfo.endTime}");
            }

            String saleNames = CommonUtilAbstract.getSaleNames();
            if (StringUtils.isNotBlank(saleNames)) {
                sql.WHERE(" oneAi.sale_name in ("+saleNames+")");
            }

        }
    }

}
